home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_7 / issue_07 / drawfile / !RendDraw / c / RendDraw < prev   
Encoding:
Text File  |  1993-06-21  |  8.0 KB  |  338 lines

  1. /*
  2.  * Name   : RendDraw.c
  3.  * Desc   : Main control for the RendDraw application
  4.  * Author : James Bye
  5.  * Date   : 6th April 1992
  6.  *
  7.  *
  8.  * **********************************************
  9.  * * Disclaimer                                 *
  10.  * * ----------                                 *
  11.  * *                                            *
  12.  * * The software is provided "as is" Acorn     *
  13.  * * Computers Limited ("Acorn") makes no       *
  14.  * * warranty, express or implied, of the       *
  15.  * * merchantability of this software or its    *
  16.  * * fitness for any particular purpose. In no  *
  17.  * * circumstances shall Acorn be liable for any*
  18.  * * damage, loss of profits, or any indirect or*
  19.  * * consequential loss arising out of the use  *
  20.  * * of this software or inability to use this  *
  21.  * * software, even if Acorn has been advised of*
  22.  * * the possibility of such loss.              *
  23.  * **********************************************
  24.  */
  25.  
  26. #include "wimp.h"        /*  access to WIMP SWIs                      */
  27. #include "wimpt.h"       /*  wimp task facilities                     */
  28. #include "win.h"         /*  registering window handlers              */
  29. #include "event.h"       /*  poll loops, etc                          */
  30. #include "baricon.h"     /*  putting icon on icon bar                 */
  31. #include "sprite.h"      /*  sprite operations                        */
  32. #include "werr.h"        /*  error reporting                          */
  33. #include "res.h"         /*  access to resources                      */
  34. #include "resspr.h"      /*  sprite resources                         */
  35. #include "flex.h"        /*  dynamic mem alloc from WIMP              */
  36. #include "template.h"    /*  reading in template file                 */
  37. #include "bbc.h"         /*  olde-style graphics routines             */
  38. #include "colourtran.h"  /*  interface to colour translation module   */
  39. #include "os.h"          /*  low-level RISCOS access                  */
  40. #include "dbox.h"        /*  dialogue box handling                    */
  41. #include "saveas.h"      /*  data export from dbox by icon dragging   */
  42. #include "visdelay.h"    /*  show the hourglass for delay             */ 
  43.                   
  44. #define  TRACE 1
  45. #include "trace.h"
  46.  
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <stdio.h>    
  50.  
  51. #include "drawfile.h"
  52. #include "file.h"
  53.  
  54.  
  55. /*-- menu definitions --*/
  56.  
  57. #define m_IconBar_Title "RendDraw"
  58. #define m_IconBar_Hits  ">Info,Quit"
  59. #define m_IconBar_Info  1
  60. #define m_IconBar_Quit  2
  61.  
  62. /*-- static variables --*/
  63.  
  64. static menu   iconbar_menu;
  65. static wimp_w display;
  66. static BOOL   display_open = FALSE;
  67.  
  68. static DrawFile_Data drawfile = NULL;
  69. static int   drawfile_size = 0;      
  70.  
  71. #define XCoord 0
  72. #define YCoord 0
  73.  
  74. /**************************************************
  75.  * Static functions                               *
  76.  **************************************************/
  77.  
  78. /*
  79.  * loads the draw file
  80.  */
  81.  
  82. static BOOL load_drawfile ( char *filename )
  83. {                                    
  84. _kernel_oserror *e;
  85.  
  86.   /*-- read the size of the draw file --*/
  87.  
  88.   e = file_readcat(filename,NULL,NULL,NULL,&drawfile_size,NULL);
  89.  
  90.   if(e != NULL)
  91.   {
  92.     werr(0,"%s",e->errmess);
  93.     return(FALSE);
  94.   }
  95.  
  96.   drawfile = malloc(drawfile_size);
  97.  
  98.   if(!drawfile)
  99.   {
  100.     werr(0,"Malloc failed");
  101.     drawfile_size = 0;
  102.     return(FALSE);
  103.   }
  104.  
  105.   /*-- load the file and set up structures --*/
  106.  
  107.   file_load(filename,(int)drawfile);                
  108.  
  109.   return(TRUE);
  110. }
  111.  
  112.  
  113.   
  114.  
  115.  
  116.  
  117. /*
  118.  * redraw handle for the display window
  119.  */
  120.  
  121. static void display_redraw ( void )
  122. {
  123. wimp_redrawstr r;
  124. BOOL more = FALSE;
  125. DrawFile_ClipBBoxStr clip;
  126.                   
  127.   r.w = display;
  128.   wimp_redraw_wind(&r,&more);
  129.  
  130.   while(more)
  131.   {                       
  132.     memcpy(&clip,&r.g,sizeof(DrawFile_ClipBBoxStr));
  133.     DrawFile_Render(0 | DrawFile_RenderBoundingBoxes,
  134.          drawfile,drawfile_size,
  135.          DrawFile_UseIdentityTransMat,&clip,
  136.          r.box.x0 - r.scx,r.box.y1 - r.scy);
  137.     wimp_get_rectangle(&r,&more);
  138.   }
  139. }
  140.  
  141.  
  142. /*
  143.  * event handler for the display window
  144.  */
  145.  
  146. static void display_events ( wimp_eventstr *e, void *handle )
  147. {
  148.   handle = handle;
  149.  
  150.   switch(e->e)
  151.   {
  152.     case wimp_EOPEN  : wimp_open_wind(&e->data.o);
  153.                        break;
  154.     case wimp_ECLOSE : wimp_close_wind(display);
  155.                        wimp_delete_wind(display);
  156.                        display_open = FALSE;
  157.                        if(drawfile)
  158.                          free(drawfile);
  159.                        break;
  160.     case wimp_EREDRAW: display_redraw();
  161.                        break;
  162.   }
  163. }
  164.  
  165.  
  166. /*
  167.  * shows the display window
  168.  */                                
  169.  
  170. static void show_display ( char *filename )
  171. {
  172.   if(!display_open)
  173.   {
  174.     wimp_wind *wind = template_syshandle("display");
  175.     wimp_wstate state;
  176.  
  177.      /*-- and create the window --*/
  178.  
  179.      wimp_create_wind(wind,&display);
  180.  
  181.      /*-- get window state and open the window --*/
  182.  
  183.      wimp_get_wind_state(display,&state);
  184.      state.o.behind = -1;
  185.      wimp_open_wind(&state.o);
  186.  
  187.      /*-- attach our event handler --*/
  188.  
  189.      win_register_event_handler(display,display_events,NULL);
  190.      display_open = TRUE;
  191.   }
  192.   else
  193.    werr(0,"Already displaying");
  194. }
  195.  
  196.  
  197.  
  198. /*
  199.  * shows the info box
  200.  */
  201.  
  202. extern void show_info ( void )
  203. {
  204. dbox d = dbox_new("info");
  205.  
  206.   if(d == NULL)
  207.     werr(1,"No memory for DBox");
  208.   dbox_show(d);
  209.   dbox_fillin(d);
  210.   dbox_dispose(&d);
  211. }
  212.  
  213.  
  214.  
  215. /*
  216.  * click proc for the iconbar
  217.  */
  218.  
  219. static void click_proc ( wimp_i i )
  220. {
  221.   i = i;
  222.   werr(0,"Drag a draw file to the RendDraw icon");
  223. }
  224.  
  225.  
  226. /*
  227.  * menu handler for the iconbar
  228.  */
  229.  
  230. static void iconbar_menu_events ( void *handle, char *hit )
  231. {                          
  232.   handle = handle;
  233.   switch(hit[0])
  234.   {
  235.      case m_IconBar_Info : show_info();
  236.                            break;
  237.      case m_IconBar_Quit : exit(0);
  238.                            break;
  239.      default             : break;
  240.   }
  241. }
  242.  
  243.  
  244. /*
  245.  * loader for the iconbar
  246.  */
  247.  
  248. static void iconbar_loader ( wimp_eventstr *e, void *handle )
  249. {
  250.   handle = handle;
  251.   switch(e->e)
  252.   {
  253.      case wimp_ESEND :
  254.      case wimp_ESENDWANTACK :
  255.  
  256.        switch(e->data.msg.hdr.action)
  257.        {
  258.          case wimp_MDATALOAD :
  259.          case wimp_MDATASAVE :
  260.          case wimp_MDATAOPEN : /*-- load if it is a drawfile --*/
  261.                                if(e->data.msg.data.dataload.type == 0xAFF)
  262.                                {
  263.                                  if(load_drawfile(e->data.msg.data.dataload.name))
  264.                                    show_display(e->data.msg.data.dataload.name);
  265.                                }
  266.                                else
  267.                                  werr(0,"RendDraw can only display drawfiles");
  268.                                break;
  269.          default             : break;
  270.        } break;
  271.  
  272.      default : break;
  273.   }
  274. }
  275.   
  276.  
  277. /*
  278.  * unknown event handler for the application
  279.  */
  280.  
  281. static BOOL our_unknowns ( wimp_eventstr *e, void *handle )
  282. {
  283.   handle = handle;
  284.   switch(e->e)
  285.   {
  286.     case wimp_MMODECHANGE : /*-- recache mode vars on mode change --*/
  287.                             wimpt_checkmode();                        
  288.                             return(TRUE);
  289.                             break;
  290.   }
  291.   return(FALSE);
  292. }
  293.  
  294.  
  295. /**************************************************
  296.  * Main C function                                *
  297.  **************************************************/
  298.  
  299. int main ( void )
  300. {
  301.   /*-- init ourselves with the wimp --*/
  302.  
  303.   wimpt_init("Render Drawfile");
  304.   res_init("RendDraw");
  305.   resspr_init();
  306.   flex_init();
  307.   template_init();
  308.   dbox_init();
  309.   visdelay_init();
  310.  
  311.   trace_on();
  312.  
  313.   /*-- place the icon on the iconbar --*/
  314.  
  315.   baricon("!RendDraw",1,click_proc);
  316.  
  317.   /*-- create & attach the menu to the iconbar --*/
  318.                 
  319.   iconbar_menu = menu_new(m_IconBar_Title,m_IconBar_Hits);
  320.   event_attachmenu(win_ICONBAR,iconbar_menu,iconbar_menu_events,NULL);
  321.  
  322.   /*-- attach unknown event handler for the application --*/ 
  323.  
  324.   win_add_unknown_event_processor(our_unknowns,NULL);
  325.  
  326.   /*-- attach an event handler to the iconbar --*/
  327.  
  328.   win_register_event_handler(win_ICONBARLOAD,iconbar_loader,NULL);
  329.   
  330.   /*-- inifinite polling loop --*/
  331.  
  332.   while(TRUE)
  333.     event_process();
  334. }
  335.  
  336. /*-- end --*/
  337.  
  338.